package yc.bluetooth.ycvoiceprotest.pop;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import yc.bluetooth.ycvoiceprotest.R;
import yc.bluetooth.ycvoiceprotest.adapter.LVDevicesAdapter;
import yc.bluetooth.ycvoiceprotest.constant.Cons;
import yc.bluetooth.ycvoiceprotest.data.BDevice;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

public class PopSearchDevice {

    private static final String TAG = "PopSearchDevice" ;

    private Context context;
    private PopupWindow popupWindow;
    private View view;

    private ListView listView;
    private ProgressBar pbSearching;
    private TextView tvCancel;

    private List<BDevice> deviceList = new ArrayList<>();
    private LVDevicesAdapter lvDevicesAdapter;


    public PopSearchDevice(Context context) {
        this.context = context;
        view = View.inflate(context, R.layout.layout_pop_search_device,null);
        popupWindow = new PopupWindow(view,MATCH_PARENT,MATCH_PARENT, true);

        listView = view.findViewById(R.id.lv_devices);
        pbSearching = view.findViewById(R.id.pb_searching);
        tvCancel = view.findViewById(R.id.tv_cancel);
        showProgress();

        lvDevicesAdapter = new LVDevicesAdapter(context,deviceList);
        listView.setAdapter(lvDevicesAdapter);


        //设备列表点击
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(popListener != null) {
                    popListener.onClickItem((BDevice) lvDevicesAdapter.getItem(i));
                }
                close();

            }
        });

        //取消搜索
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                close();
            }
        });

    }

    public void showPop() {
        if (popupWindow == null) {
            return;
        }

        //清空设备列表
        if (lvDevicesAdapter != null){
            lvDevicesAdapter.clear();
        }

        popupWindow.showAtLocation(((Activity)context).getWindow().getDecorView(), Gravity.CENTER,0,0);

    }

    /**
     * 添加新扫描到的设备
     * @param bDevice
     */
    public void addListItem(BDevice bDevice){
        Log.e(TAG,"addListItem");
        if(lvDevicesAdapter != null){
            String deviceName = bDevice.bluetoothDevice.getName();
            //BLE_JU_RMC
            Log.e(TAG,"addListItem>>deviceName="+deviceName);
            if( deviceName != null){
                lvDevicesAdapter.addDevice(bDevice);
            }
        }
    }

    /**
     * 显示加载圈
     */
    public void showProgress(){
        pbSearching.setVisibility(View.VISIBLE);
    }


    /**
     * 隐藏加载圈
     */
    public void hideProgress(){
        pbSearching.setVisibility(View.GONE);
    }

    private void close(){
        if (popupWindow == null)
            return;

        popupWindow.dismiss();

        if(popListener != null) {
            popListener.onClosePop();
        }

    }

    public boolean isShowing(){
        if (popupWindow == null)
            return false;

        return popupWindow.isShowing();
    }

    public interface PopListener{
        void onClickItem(BDevice bDevice);
        void onClosePop();
    }
    private  PopListener popListener;

    public void setPopListener(PopListener popListener) {
        this.popListener = popListener;
    }
}
